package com.starry.home.ui.frament

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.starry.core.base.BaseFragment
import com.starry.core.constant.GlobalConfig
import com.starry.core.ext.singleClick
import com.starry.core.ext.toast
import com.starry.core.utils.SpUtil
import com.starry.home.databinding.FragmentHomeBinding
import com.starry.home.ui.hospital.HospitalActivity
import com.starry.home.ui.main.MedicalActivity
import com.starry.home.ui.main.PreviewActivity
import com.starry.home.viewmodel.main.MainViewModel


/**
 * Package:com.starry.dotpen_patients.ui.main
 * Author:starr
 * Time:2024/1/4 15:11
 * Description: 患者端首页
 */
class HomeFragment : BaseFragment<FragmentHomeBinding, MainViewModel>() {


    companion object {
        private const val PARAM = "param"
        fun newInstance(param: String?): HomeFragment {
            val fragment = HomeFragment()
            val args = Bundle()
            args.putString(PARAM, param)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getRecently(SpUtil.getIntValue(GlobalConfig.userId))
    }

    override fun createObserver() {
        mViewModel.recently.observe(this) {
            if (it.isSuccess) {
                val model = it.getOrNull()
                model?.let {
                    mDataBinding.readTimeTv.text = model.ghList.size.toString()
                }
            } else {
                toast("获取最近预约失败")
            }
        }
    }

    override fun initData() {
        mDataBinding.click = HomeClick()
        mViewModel.getRecently(SpUtil.getIntValue(GlobalConfig.userId))
        mDataBinding.llTime.singleClick {
            PreviewActivity.startAction(mContext)
        }
        mDataBinding.tvSearch.singleClick {
            ARouter.getInstance().build("/chat/ui/ChatActivity").navigation();
        }
    }

    override fun initView() {
    }

    inner class HomeClick {

        //预约挂号
        fun register(view: View?) {
            HospitalActivity.startAction(mContext)
        }

        //我的预约
        fun preview(view: View?) {
            PreviewActivity.startAction(mContext)
        }

        //我的病历
        fun medical(view: View?) {
            MedicalActivity.startAction(mContext)
        }
    }
}